package com.xuhuan.consumer.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author huan.xu
 * @ClassName HttpCallUtil
 * @date 2021/6/4 8:28
 * @Description 发起 http 请求工具类
 */
@Slf4j
public class HttpCallUtil {
    /**
     * 私有构造方法
     */
    private HttpCallUtil() {

    }

    /**
     * 发起 get 请求
     *
     * @param restTemplate RestTemplate 对象
     * @param url          请求 url
     * @param paramMap     请求参数
     * @return
     */
    public static String sendGet(RestTemplate restTemplate, String url, Map<String, Object> paramMap) {
        log.info("--------发起get请求--------");
        log.info("请求地址[{}]", url);
        log.info("请求参数[{}]", paramMap);

        StringBuilder urlBuilder = new StringBuilder(url);

        if (!paramMap.isEmpty()) {
            boolean flag = false;
            for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
                String key = entry.getKey();
                String value = (String) entry.getValue();
                if (!flag) {
                    urlBuilder.append("?");
                    flag = true;
                } else {
                    urlBuilder.append("&");
                }
                urlBuilder.append(key).append("=").append(value);
            }
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("token", "tokenValue");

        HttpEntity<Map<String, Object>> request = new HttpEntity<>(null, headers);

        log.info("完整的get请求url[{}]", urlBuilder.toString());
        ResponseEntity<String> responseEntity = restTemplate.exchange(urlBuilder.toString(), HttpMethod.GET, request, String.class);
        HttpStatus statusCode = responseEntity.getStatusCode();
        log.info("请求状态[{}]", statusCode);
        String body = responseEntity.getBody();
        log.info("请求结果[{}]", body);
        return body;
    }

    /**
     * 发起 post 请求
     *
     * @param restTemplate RestTemplate 对象
     * @param url          请求 url
     * @param paramMap     请求参数
     * @return
     */
    public static String sendPost(RestTemplate restTemplate, String url, Map paramMap) {
        log.info("--------发起post请求--------");
        log.info("请求地址[{}]", url);
        log.info("请求参数[{}]", paramMap);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("token", "tokenValue");

        HttpEntity<Map<String, Object>> request = new HttpEntity<>(paramMap, headers);

        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
        HttpStatus statusCode = responseEntity.getStatusCode();
        log.info("请求状态[{}]", statusCode);
        String body = responseEntity.getBody();
        log.info("请求结果[{}]", body);
        return body;
    }

}
